
#include "Commands.h"
#include <string>

const int MAX_COMMAND_SIZE = 15;

void assemble(FILE* input, FILE* output);
void try_to_translate(string cmd, command_t command, FILE* input, FILE* output);

#define TRY_TRSLT(COMMAND) try_to_translate(cmd, COMMAND, input, output)

void assemble(FILE* input, FILE* output)
{
    char cmd[MAX_COMMAND_SIZE];

    fscanf(input, "%s", &cmd);

    while(cmd != END.name)
    {
        TRY_TRSLT(PUSH);
        TRY_TRSLT(ADD);
        TRY_TRSLT(SUB);
        TRY_TRSLT(MUL);
        TRY_TRSLT(DIV);
        TRY_TRSLT(SIN);
        TRY_TRSLT(COS);
        TRY_TRSLT(SQRT);
        TRY_TRSLT(PUSHX);

        fscanf(input, "%s", &cmd);
    }

    fprintf(output, "%d\n", END.code);
}

void try_to_translate(string cmd, command_t command, FILE* input, FILE* output)
{
    if(cmd == command.name)
    {
        fprintf(output, "%d\n", command.code);

        for(int i = 0; i < command.n_args; i++)
        {
            float arg = 0;
            fscanf (input,  "%g",  &arg);

            //printf("Argument is %g\n", arg);

            fprintf(output, "%g\n", arg);
        }
    }
}




